package org.rsbot.script.wrappers;

import java.awt.Point;
import org.rsbot.client.Client;
import org.rsbot.client.Graphic;
import org.rsbot.client.Model;
import org.rsbot.client.Node;
import org.rsbot.client.RSNPCNode;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.MethodContext;
import org.rsbot.script.methods.MethodProvider;
import org.rsbot.script.methods.Nodes;

public abstract class RSCharacter extends MethodProvider
{
  public RSCharacter(MethodContext paramMethodContext)
  {
    super(paramMethodContext);
  }

  protected abstract org.rsbot.client.RSCharacter getAccessor();

  public boolean doAction(String paramString)
  {
    RSModel localRSModel = getModel();
    if ((localRSModel != null) && (isValid())) {
      return getModel().doAction(paramString);
    }
    return false;
  }

  public RSModel getModel() {
    org.rsbot.client.RSCharacter localRSCharacter = getAccessor();
    if (localRSCharacter != null) {
      Model localModel = localRSCharacter.getModel();
      if (localModel != null) {
        return new RSCharacterModel(this.methods, localModel, localRSCharacter);
      }
    }
    return null;
  }

  public int getAnimation() {
    return getAccessor().getAnimation();
  }

  public int getGraphic() {
    return getAccessor().getGraphicsData()[0].getID();
  }

  public int getHeight() {
    return getAccessor().getHeight();
  }

  public int getHPPercent()
  {
    return isInCombat() ? getAccessor().getHPRatio() * 100 / 255 : 100;
  }

  public RSCharacter getInteracting() {
    int i = getAccessor().getInteracting();
    if (i == -1) {
      return null;
    }
    if (i < 32768) {
      Node localNode = this.methods.nodes.lookup(this.methods.client.getRSNPCNC(), i);

      if ((localNode == null) || (!(localNode instanceof RSNPCNode))) {
        return null;
      }
      return new RSNPC(this.methods, ((RSNPCNode)localNode).getRSNPC());
    }
    int j = i - 32768;
    if (j == this.methods.client.getSelfInteracting()) {
      j = 2047;
    }
    return new RSPlayer(this.methods, this.methods.client.getRSPlayerArray()[j]);
  }

  public RSTile getLocation()
  {
    org.rsbot.client.RSCharacter localRSCharacter = getAccessor();
    if (localRSCharacter == null) {
      return new RSTile(-1, -1);
    }
    int i = this.methods.client.getBaseX() + (localRSCharacter.getX() >> 9);
    int j = this.methods.client.getBaseY() + (localRSCharacter.getY() >> 9);
    return new RSTile(i, j);
  }

  public String getMessage() {
    return getAccessor().getMessage();
  }

  public Point getMinimapLocation()
  {
    org.rsbot.client.RSCharacter localRSCharacter = getAccessor();
    int i = this.methods.client.getBaseX() + (localRSCharacter.getX() / 32 - 2) / 4;
    int j = this.methods.client.getBaseY() + (localRSCharacter.getY() / 32 - 2) / 4;
    return this.methods.calc.worldToMinimap(i, j);
  }

  public String getName() {
    return null;
  }

  public int getLevel() {
    return -1;
  }

  public int getOrientation() {
    return (int)(270.0D - (getAccessor().getOrientation() & 0x3FFF) / 45.509999999999998D) % 360;
  }

  public Point getScreenLocation() {
    org.rsbot.client.RSCharacter localRSCharacter = getAccessor();
    RSModel localRSModel = getModel();
    if (localRSModel == null) {
      return this.methods.calc.groundToScreen(localRSCharacter.getX(), localRSCharacter.getY(), localRSCharacter.getHeight() / 2);
    }

    return localRSModel.getPoint();
  }

  public void hover()
  {
    getModel().hover();
  }

  public boolean isInCombat() {
    return (this.methods.game.isLoggedIn()) && (this.methods.client.getLoopCycle() < getAccessor().getLoopCycleStatus());
  }

  public boolean isInteractingWithLocalPlayer()
  {
    return getAccessor().getInteracting() - 32768 == this.methods.client.getSelfInteracting();
  }

  public boolean isMoving()
  {
    return getAccessor().isMoving() != 0;
  }

  public boolean isOnScreen() {
    RSModel localRSModel = getModel();
    if (localRSModel == null) {
      return this.methods.calc.tileOnScreen(getLocation());
    }
    return this.methods.calc.pointOnScreen(localRSModel.getPoint());
  }

  public boolean isValid()
  {
    return getAccessor() != null;
  }

  public int hashCode()
  {
    return System.identityHashCode(getAccessor());
  }

  public boolean equals(Object paramObject)
  {
    if ((paramObject instanceof RSCharacter)) {
      RSCharacter localRSCharacter = (RSCharacter)paramObject;
      return localRSCharacter.getAccessor() == getAccessor();
    }
    return false;
  }

  public String toString()
  {
    RSCharacter localRSCharacter = getInteracting();
    return new StringBuilder().append("[anim=").append(getAnimation()).append(",msg=").append(getMessage()).append(",interact=").append(localRSCharacter.isValid() ? localRSCharacter.getMessage() : localRSCharacter == null ? "null" : "Invalid").append("]").toString();
  }
}